Page({
    data: {
        students: [
            {
                id: 1,
                name: '张三',
                studentId: '2023001',
                class: '计算机科学与技术1班',
                avatar: '/images/avatar1.png',
                phone: '138****8888',
                paymentStatus: 'paid',
                totalAmount: 7380.00,
                paidAmount: 7380.00,
                unpaidAmount: 0,
                courseProgress: {
                    total: 4,
                    completed: 1,
                    learning: 2,
                    notStarted: 1
                },
                lastLoginTime: '2024-01-15 14:30'
            },
            {
                id: 2,
                name: '李四',
                studentId: '2023002',
                class: '计算机科学与技术1班',
                avatar: '/images/avatar2.png',
                phone: '139****9999',
                paymentStatus: 'partial',
                totalAmount: 7380.00,
                paidAmount: 5800.00,
                unpaidAmount: 1580.00,
                courseProgress: {
                    total: 4,
                    completed: 0,
                    learning: 2,
                    notStarted: 2
                },
                lastLoginTime: '2024-01-14 09:15'
            },
            {
                id: 3,
                name: '王五',
                studentId: '2023003',
                class: '计算机科学与技术2班',
                avatar: '/images/avatar3.png',
                phone: '137****7777',
                paymentStatus: 'unpaid',
                totalAmount: 7380.00,
                paidAmount: 0,
                unpaidAmount: 7380.00,
                courseProgress: {
                    total: 4,
                    completed: 0,
                    learning: 0,
                    notStarted: 4
                },
                lastLoginTime: '2024-01-10 16:45'
            },
            {
                id: 4,
                name: '赵六',
                studentId: '2023004',
                class: '计算机科学与技术2班',
                avatar: '/images/avatar4.png',
                phone: '136****6666',
                paymentStatus: 'paid',
                totalAmount: 7380.00,
                paidAmount: 7380.00,
                unpaidAmount: 0,
                courseProgress: {
                    total: 4,
                    completed: 2,
                    learning: 1,
                    notStarted: 1
                },
                lastLoginTime: '2024-01-15 11:20'
            }
        ],
        filterStatus: 'all', // all, paid, unpaid, partial
        searchKeyword: '',
        filteredStudents: []
    },

    onLoad() {
        this.setData({
            filteredStudents: this.data.students
        });
    },

    onShow() {
        // 检查用户角色
        const userRole = wx.getStorageSync('userRole');
        if (userRole !== 'teacher') {
            wx.redirectTo({
                url: '/pages/role-select/role-select'
            });
        }
    },

    // 筛选学生
    filterStudents(e) {
        const status = e.currentTarget.dataset.status;
        this.setData({
            filterStatus: status
        });
        this.applyFilter();
    },

    // 搜索学生
    onSearchInput(e) {
        this.setData({
            searchKeyword: e.detail.value
        });
        this.applyFilter();
    },

    // 应用筛选和搜索
    applyFilter() {
        const { students, filterStatus, searchKeyword } = this.data;
        let filtered = students;

        // 按缴费状态筛选
        if (filterStatus !== 'all') {
            filtered = filtered.filter(student => student.paymentStatus === filterStatus);
        }

        // 按关键词搜索
        if (searchKeyword) {
            filtered = filtered.filter(student =>
                student.name.includes(searchKeyword) ||
                student.studentId.includes(searchKeyword) ||
                student.class.includes(searchKeyword)
            );
        }

        this.setData({
            filteredStudents: filtered
        });
    },

    // 查看学生详情
    viewStudentDetail(e) {
        const studentId = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/teacher/student-detail/student-detail?studentId=${studentId}`
        });
    },

    // 批量操作
    batchOperation() {
        wx.showActionSheet({
            itemList: ['导出学生信息', '发送通知', '批量催缴'],
            success: (res) => {
                switch (res.tapIndex) {
                    case 0:
                        this.exportStudentInfo();
                        break;
                    case 1:
                        this.sendNotification();
                        break;
                    case 2:
                        this.remindPayment();
                        break;
                }
            }
        });
    },

    // 导出学生信息
    exportStudentInfo() {
        wx.showToast({
            title: '导出功能开发中',
            icon: 'none'
        });
    },

    // 发送通知
    sendNotification() {
        wx.showToast({
            title: '通知功能开发中',
            icon: 'none'
        });
    },

    // 催缴提醒
    remindPayment() {
        const unpaidStudents = this.data.students.filter(s => s.paymentStatus !== 'paid');
        wx.showModal({
            title: '催缴提醒',
            content: `将向${unpaidStudents.length}名学生发送缴费提醒`,
            success: (res) => {
                if (res.confirm) {
                    wx.showToast({
                        title: '提醒已发送',
                        icon: 'success'
                    });
                }
            }
        });
    },

    // 切换到学生角色
    switchToStudent() {
        wx.showModal({
            title: '切换身份',
            content: '确定要切换到学生身份吗？',
            success: (res) => {
                if (res.confirm) {
                    wx.setStorageSync('userRole', 'student');
                    wx.switchTab({
                        url: '/pages/profile/profile'
                    });
                }
            }
        });
    }
}); 